package com.itlong.cloud.propertyapp.service.impl;

import com.itlong.cloud.POJO.DTO.app.property.app.PropertyAppInspectDTO;
import com.itlong.cloud.POJO.PO.PropertyMessagePO;
import com.itlong.cloud.POJO.PO.PropertyMessageReadRecordPO;
import com.itlong.cloud.POJO.VO.app.property.app.PropertyAppMessageVO;
import com.itlong.cloud.POJO.VO.app.property.app.PropertyAppNoticeInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.propertyapp.dao.IPropertyAppMessageDao;
import com.itlong.cloud.propertyapp.service.IPropertyAppMessageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <desc>
 *      物业APP消息模块业务逻辑实现类
 * </desc>
 *
 * @createDate2020/1/3
 **/
@Service
public class PropertyAppMessageServiceImpl implements IPropertyAppMessageService {

    @Autowired
    private IPropertyAppMessageDao iPropertyAppMessageDao;
    /**
     * <desc>
     *      保存公告/内部通知信息
     * </desc>
     *
     * @param projectId 项目id
     * @param title 标题
     * @param postMan 发布人
     * @param content 发布内容
     * @return
     * @Author Zhu.yj
     * @createDate  2020/1/3
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer postNotice(String projectId, String title, String postMan, String content,String userId) throws Exception{
        PropertyMessagePO propertyMessagePO = new PropertyMessagePO();
        propertyMessagePO.setProjectId(projectId);
        propertyMessagePO.setAuthor(postMan);
        propertyMessagePO.setTitle(title);
        propertyMessagePO.setContent(content);
        propertyMessagePO.setMessageId(LogicIdUtil.bussinessId());
        propertyMessagePO.setCreateUserId(userId);
        propertyMessagePO.setCreateTime(new Date());
        propertyMessagePO.setStatus(DataStatusEnum.NORMAL.getType());
        Integer result = iPropertyAppMessageDao.save(SqlUtil.durableData(propertyMessagePO, PlatformConstants.TABLE_SAVE));
        return result;
    }

    /**
     * <desc>
     *      获取公告列表
     * </desc>
     *
     * @param projectId 项目id
     * @param pageNum 页码
     * @return
     * @Author Zhu.yj
     * @createDate  2020/1/3
     */
    @Override
    public Map<String, Object> getNoticeList(String projectId, String pageNum,String userId) throws Exception {
        Map<String, Object> params = new HashMap<>();
        if (Integer.parseInt(pageNum)>1){
            params.put("startSlip",(Integer.parseInt(pageNum) - 1) * 15);
        }else {
            params.put("startSlip",0);
        }
        params.put("pageSize",15);
        params.put("projectId",projectId);
        params.put("userId",userId);
        List<PropertyAppNoticeInfoVO> propertyAppNoticeInfoVOS = iPropertyAppMessageDao.getNoticeList(params);
        int count = iPropertyAppMessageDao.getNoticeListCount(params);
        int totalPage = count == 0 ? 0 : count / 15 + 1;
        Map<String, Object> result = new HashMap<>();
        result.put("totNum",totalPage);
        result.put("noticeAry",propertyAppNoticeInfoVOS);
        return result;
    }

    /**
     * <desc>
     *      获取消息详情，标记已读信息
     * </desc>
     *
     * @param projectId 项目id
     * @param noticeId 消息id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/1/3
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> getNoticeDetail(String projectId, String noticeId,String userId) throws Exception{
        //获取消息详情
        Map<String,Object> resultMap = iPropertyAppMessageDao.getNoticeDetail(projectId,noticeId);
        //查看该用户是否已读该消息，是则不操作，否则添加记录
        Integer count = iPropertyAppMessageDao.userNoticeCount(userId,noticeId);
        if (count == 0){
            //执行插入操作
            PropertyMessageReadRecordPO propertyMessageReadRecordPO = new PropertyMessageReadRecordPO();
            propertyMessageReadRecordPO.setMessageId(noticeId);
            propertyMessageReadRecordPO.setUserId(userId);
            propertyMessageReadRecordPO.setStatus(DataStatusEnum.NORMAL.getType());
            propertyMessageReadRecordPO.setCreateTime(new Date());
            iPropertyAppMessageDao.save(SqlUtil.durableData(propertyMessageReadRecordPO,PlatformConstants.TABLE_SAVE));
        }
        return resultMap;
    }

    /**
     * <desc>
     *      获取消息中心列表
     * </desc>
     *
     * @return 消息中心列表
     * @author wangzhi
     * @createDate 2020/02/17
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> getMessagesList(PropertyAppInspectDTO propertyAppInspectDTO) throws Exception {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        params.put("projectId",propertyAppInspectDTO.getProjectId());
        params.put("userId",propertyAppInspectDTO.getUserId());
        // 分页索引
        if (Integer.parseInt(propertyAppInspectDTO.getPageNum()) > 1) {
            params.put("startSlip",(Integer.parseInt(propertyAppInspectDTO.getPageNum()) - 1) * 15);
        } else {
            params.put("startSlip", 0);
        }
        // 分页记录数
        params.put("pageSize",15);
        List<PropertyAppMessageVO> propertyAppMessageVOList = iPropertyAppMessageDao.getMessagePage(params);
        if (propertyAppMessageVOList != null && !propertyAppMessageVOList.isEmpty()) {
            for (PropertyAppMessageVO propertyAppMessageVO : propertyAppMessageVOList) {
                propertyAppMessageVO.setUrl(propertyAppInspectDTO.getUrl() + "pushInfoId=" + propertyAppMessageVO.getMsgId());
            }
            Integer unReadCount = iPropertyAppMessageDao.getUnReadCount(params);
            data.put("unReadCount", unReadCount.toString());
        } else {
            propertyAppMessageVOList = propertyAppMessageVOList == null ? new ArrayList<>() : propertyAppMessageVOList;
            data.put("unReadCount", "0");
        }
        data.put("msgAry", propertyAppMessageVOList);
        return data;
    }

    /**
     * <desc>
     *      获取公告消息内容列表
     * </desc>
     *
     * @param projectId 项目id
     * @return
     * @Author Zhu.yj
     * @createDate  2020/3/13
     */
    @Override
    public List<String> getNoticeContent(String projectId) throws Exception {
        return iPropertyAppMessageDao.getNoticeContent(projectId);
    }
}
